package com.capgemini.scaffold.controller;

import com.capgemini.scaffold.common.ResponseBean;
import com.capgemini.scaffold.dto.LoginFromDTO;
import com.capgemini.scaffold.dto.UserDTO;
import com.capgemini.scaffold.entity.User;
import com.capgemini.scaffold.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author ShuHao.Wen
 * @since 2022/10/21
 */
@Api("用户管理")
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public ResponseBean<LoginFromDTO> login(@RequestBody UserDTO userDTO) {
        return ResponseBean.success(userService.login(userDTO));
    }

    @ApiOperation("用户注册")
    @PostMapping("/signIn")
    public ResponseBean<UserDTO> signIn(@RequestBody UserDTO userDTO) {
         return ResponseBean.success(userService.signIn(userDTO));
    }

    @ApiOperation("通过id查询用户信息")
    @GetMapping("/{id}")
    public ResponseBean<LoginFromDTO> queryUser(@PathVariable("id") Integer id) {
        return ResponseBean.success(userService.queryUserById(id));
    }

    @ApiOperation("修改用户信息")
    @PutMapping
    public ResponseBean<User> updateUser( @RequestBody UserDTO userDTO) {
        return ResponseBean.success(userService.updateUserById( userDTO));
    }

    @ApiOperation("退出登录")
    @DeleteMapping
    public void delCache(){
        /**
         * 一方面就是查询到的数据和登录用户数据不匹配
         * 数据积压问题
         */
        userService.removeCache();
    }


}
